package org.example.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.example.bean.CurrentData;
import org.example.mapper.CurrentDataMapper;
import org.example.utils.ExcelUtils;
import org.example.utils.PubFun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/main")
public class MainController {

    @Autowired
    private CurrentDataMapper currentDataMapper;

    @GetMapping("/hello")
    public String getData() {
        return "Hello World";
    }

    @GetMapping("/db")
    public String db() {
        List<CurrentData> currentData = currentDataMapper.selectList(null);
        System.out.println(currentData);
        return currentData.size()+"";
    }

    @GetMapping("/sb")
    public void sb( HttpServletResponse response) {
        List<CurrentData> currentData = currentDataMapper.selectList(null);
        System.out.println(currentData.size());
        HashSet<String> set = new HashSet<>();
        //先把所有的设备标识都放到set中去重
        for (CurrentData data : currentData) {
            set.add(data.getDeviceIdentifier());
        }

        //将所有的设备标识放到map中
        HashMap<String, HashSet<String>> map = new HashMap<>();
        for (String s : set) {
            String gatewayId = PubFun.getGatewayId(s);
            if(!map.containsKey(gatewayId)){
                //如果不包含
                HashSet<String> list = new HashSet<>();
                list.add(s);
                map.put(gatewayId,list);
            }else {
                HashSet<String> strings = map.get(gatewayId);
                strings.add(s);
            }
        }
        JSONArray arr = new JSONArray();
        //这样就为每一个网关都创建了一个set，set中包含了他的子设备
        System.out.println(map);
        Set<String> keys = map.keySet();
        for (String s : keys) {
            HashSet<String> strings = map.get(s);
            for(String value:strings){
                JSONObject obj = new JSONObject();
                obj.put("key",s);
                obj.put("value",value);
                arr.add(obj);
            }
        }

        exportExcel(response,arr);
    }


    public void exportExcel(HttpServletResponse response, JSONArray arr) {
        // 列名，表格第一行
        List<String> rowNameList = Arrays.asList("设备标识", "子设备标识");

        /** 数据库数据整合 */
        // 列名 数据
        List<Map<String, String>> list = new ArrayList<>();
        for(int i=0;i<arr.size();i++){
            JSONObject item = arr.getJSONObject(i);
            Map<String, String> map = new HashMap<>();
            map.put("设备标识", item.getString("key"));
            map.put("子设备标识", item.getString("value"));
            list.add(map);
        }
        // 将需要写入Excel的数据传入
        ExcelUtils.excelPort("设备子设备对应关系", rowNameList, list, response);
    }
}
